#!/usr/bin/python

"""Project Euler Timings Printer

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import os
import re
import sys
import time
import subprocess

def main():
    #The answers to the questions.
    answers = {
        1: 233168,
        2: 4613732,
        3: 6857,
        4: 906609,
        5: 232792560,
        6: 25164150,
        7: 104743,
        8: 40824,
        9: 31875000,
        10: 142913828922,
        11: 70600674,
        12: 76576500,
        13: 5537376230,
        14: 837799,
        15: 137846528820,
        16: 1366,
        17: 21124,
        18: 1074,
        19: 171,
        20: 648,
        21: 31626,
        22: 871198282,
        23: 4179871,
        24: 2783915460,
        25: 4782,
        26: 983,
        27:-59231,
        28: 669171001,
        29: 9183,
        30: 443839,
        31: 73682,
        32: 45228,
        33: 100,
        36: 872187,
        38: 932718654,
        39: 840,
        40: 210,
        41: 7652413,
        42: 162,
        44: 5482660,
        45: 1533776805,
        48: 9110846700,
        52: 142857,
        53: 4075,
        55: 249,
        56: 972,
        57: 153,
        58: 26241,
        59: 107359,
        62: 127035954683,
        63: 49,
        64: 1322,
        65: 272,
        67: 7273,
        68: 6531031914842725,
        69: 510510,
        71: 428570,
        72: 303963552392,
        73: 7295372,
        76: 190569291,
		81: 427337,
        85: 2772,
        89: 743,
        99: 709,
        102: 228
    }

    
    #The pattern of the answers files, formatted so that the name can be 
    #extracted.
    answer_file_pattern = "(?P<name>euler[0-9]{3})\.py$"
    
    #The names of the files in the answers folder.
    files = sorted(os.listdir('.'))
    
    #For every file in the answers folder.
    for script in files:
        #See if the file matches the correct pattern.
        match_object = re.match(answer_file_pattern, script)
        
        if match_object is not None:
            #Print how long it took to calculate the answer for the script. 
            #Also print if the answer is correct.
            script_name = match_object.groups("name")[0]
            
            correct_answer = answers[int(script_name.replace("euler", ""))]
            
            #Execute as a subprocess to get an accurate timing.
            start_time = time.time()
            
            pipe = subprocess.Popen(["python", script], stdout=subprocess.PIPE).stdout
            answer = float(pipe.readline().strip().strip('\n'))
            
            pipe.flush()
                        
            print(
                  "Script: {0}, Execution Time: {1:f}s, Correct: {2}"
                  .format(
                          script,
                          time.time() - start_time,
                          answer == correct_answer
                        )
                )
    
if __name__ == "__main__":
    main()
